﻿using HSPCS.BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HSPCS.Web
{
    public partial class GenControl : System.Web.UI.Page
    {
        DataBLL dataBLL = new DataBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                Literal myLiteral = new Literal();
                myLiteral.Text = "<h4>" + tableName + "</h4>";
                myPlaceHolder.Controls.Add(myLiteral);
                ShowColumn(tableName);
            }
            else
            {
                Literal myLiteral = new Literal();
                myLiteral.Text = "<h4>No table was found.</h4>";
                myPlaceHolder.Controls.Add(myLiteral);
                btnDelete.Visible = false;
                btnNext.Visible = false;
                btnPre.Visible = false;
                btnSubmit.Visible = false;
            }

        }
        public void ShowColumn(string tableName)
        {
            DataTable listColumn = new DataTable();
            listColumn = dataBLL.GetColumn(tableName);
            if (listColumn.Rows.Count > 0)
            {
                Literal myLiteralB = new Literal();
                myLiteralB.Text = "<table>";
                myPlaceHolder.Controls.Add(myLiteralB);

                foreach (DataRow r in listColumn.Rows)
                {
                    Literal myLiteral1 = new Literal();
                    myLiteral1.Text = "<tr> <td>";
                    myPlaceHolder.Controls.Add(myLiteral1);

                    Label myLabel = new Label();
                    myLabel.Text = r[0].ToString() + ": ";
                    myPlaceHolder.Controls.Add(myLabel);

                    Literal myLiteral2 = new Literal();
                    myLiteral2.Text = "</td><td>";
                    myPlaceHolder.Controls.Add(myLiteral2);

                    if (r[5].ToString() == "0")
                    {
                        TextBox myTextBox = new TextBox();
                        myTextBox.ID = "txt" + r[0].ToString();
                        myTextBox.Attributes.Add("class", "textbox");
                        myPlaceHolder.Controls.Add(myTextBox);
                    }
                    else
                    {
                        DropDownList myDropDownList = new DropDownList();
                        myDropDownList.ID = "ddl" + r[0].ToString();
                        DataTable foreignData = dataBLL.GetForeignKey(tableName, r[0].ToString());
                        if (foreignData != null)
                        {
                            myDropDownList.DataTextField = foreignData.Columns[1].ToString();
                            myDropDownList.DataValueField = foreignData.Columns[0].ToString();
                            myDropDownList.DataSource = foreignData;
                            myDropDownList.DataBind();
                            myDropDownList.Attributes.Add("style", "width:150px");
                        }
                        myPlaceHolder.Controls.Add(myDropDownList);
                    }
                    Literal myLiteral3 = new Literal();
                    myLiteral3.Text = "</td></tr>";
                    myPlaceHolder.Controls.Add(myLiteral3);
                }

                Literal myLiteralE = new Literal();
                myLiteralE.Text = "</table>";
                myPlaceHolder.Controls.Add(myLiteralE);
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                List<Dictionary<string, string>> input = new List<Dictionary<string, string>>();
                foreach (Control control in myPlaceHolder.Controls)
                {
                    if ((control is TextBox) || (control is DropDownList))
                    {
                            if (control is TextBox)
                            {
                                string colName = ((TextBox)control).ID.ToString().Substring(3);
                                string colValue = ((TextBox)control).Text.Trim();
                                Dictionary<string, string> child = new Dictionary<string, string>();
                                child.Add(colName, colValue);
                                input.Add(child);    
                            }
                            else
                            {
                                string colName = ((DropDownList)control).ID.ToString().Substring(3);
                                string colValue = ((DropDownList)control).SelectedValue.ToString().Trim();
                                Dictionary<string, string> child = new Dictionary<string, string>();
                                child.Add(colName, colValue);
                                input.Add(child);    
                            }
                    }
                }
                string result = dataBLL.SubmitData(tableName, input);
                if (!string.IsNullOrEmpty(result))
                {
                    LoadDatabyID(result);
                }
            }
        }

        public void LoadDatabyID(string key)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                string tableNameSub = tableName.Substring(4);

                if (string.IsNullOrEmpty((string)Session[tableName + "currentID"]))
                {
                    List<string> listCol = new List<string>();
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if (control is Label)
                        {
                            string label = ((Label)control).Text.Trim();
                            string subLabel = label.Substring(0, label.Length - 1);
                            listCol.Add(subLabel);
                        }
                    }

                    DataTable currentRec = dataBLL.GetRecordbyID(tableName, listCol, key);

                    if (currentRec.Rows.Count > 0)
                    {
                        foreach (Control control in myPlaceHolder.Controls)
                        {
                            if ((control is TextBox) || (control is DropDownList))
                            {
                                foreach (DataColumn colName in currentRec.Columns)
                                {
                                    if (control is TextBox)
                                    {
                                        ((TextBox)control).Text = currentRec.Rows[0][colName].ToString();
                                    }
                                    else
                                    {
                                        ((DropDownList)control).SelectedValue = currentRec.Rows[0][colName].ToString();
                                    }
                                    currentRec.Columns.Remove(colName);
                                    break;
                                }
                            }
                        }
                    }

                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;
                }
            }
        
        }
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                string tableNameSub = tableName.Substring(4);
                //Get 1st record
                if (string.IsNullOrEmpty((string)Session[tableName + "currentID"]))
                {
                    List<string> listCol = new List<string>();
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if (control is Label)
                        {
                            string label = ((Label)control).Text.Trim();
                            string subLabel = label.Substring(0, label.Length - 1);
                            listCol.Add(subLabel);
                        }
                    }

                    DataTable firstRec = dataBLL.GetFirstRecord(tableName, listCol);

                    if (firstRec.Rows.Count > 0)
                    {
                        foreach (Control control in myPlaceHolder.Controls)
                        {
                            if ((control is TextBox) || (control is DropDownList))
                            {
                                foreach (DataColumn colName in firstRec.Columns)
                                {
                                    if (control is TextBox)
                                    {
                                        ((TextBox)control).Text = firstRec.Rows[0][colName].ToString();
                                    }
                                    else
                                    {
                                        ((DropDownList)control).SelectedValue = firstRec.Rows[0][colName].ToString();
                                    }
                                    firstRec.Columns.Remove(colName);
                                    break;
                                }
                            }
                        }
                    }

                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;
                }
                else
                {
                    string currentKey = Session[tableName + "currentID"].ToString();
                    List<string> listCol = new List<string>();
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if (control is Label)
                        {
                            string label = ((Label)control).Text.Trim();
                            string subLabel = label.Substring(0, label.Length - 1);
                            listCol.Add(subLabel);
                        }
                    }

                    DataTable nextRec = dataBLL.GetNextRecord(tableName, listCol, currentKey);

                    if (nextRec.Rows.Count > 0)
                    {
                        foreach (Control control in myPlaceHolder.Controls)
                        {
                            if ((control is TextBox) || (control is DropDownList))
                            {
                                foreach (DataColumn colName in nextRec.Columns)
                                {
                                    if (control is TextBox)
                                    {
                                        ((TextBox)control).Text = nextRec.Rows[0][colName].ToString();
                                    }
                                    else
                                    {
                                        ((DropDownList)control).SelectedValue = nextRec.Rows[0][colName].ToString();
                                    }
                                    nextRec.Columns.Remove(colName);
                                    break;
                                }
                            }
                        }
                    }

                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;

                }
                
                
            }
        }

        protected void btnPre_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                string tableNameSub = tableName.Substring(4);
                //Get 1st record
                if (string.IsNullOrEmpty((string)Session[tableName + "currentID"]))
                {
                    List<string> listCol = new List<string>();
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if (control is Label)
                        {
                            string label = ((Label)control).Text.Trim();
                            string subLabel = label.Substring(0, label.Length - 1);
                            listCol.Add(subLabel);
                        }
                    }

                    DataTable firstRec = dataBLL.GetFirstRecord(tableName, listCol);

                    if (firstRec.Rows.Count > 0)
                    {
                        foreach (Control control in myPlaceHolder.Controls)
                        {
                            if ((control is TextBox) || (control is DropDownList))
                            {
                                foreach (DataColumn colName in firstRec.Columns)
                                {
                                    if (control is TextBox)
                                    {
                                        ((TextBox)control).Text = firstRec.Rows[0][colName].ToString();
                                    }
                                    else
                                    {
                                        ((DropDownList)control).SelectedValue = firstRec.Rows[0][colName].ToString();
                                    }
                                    firstRec.Columns.Remove(colName);
                                    break;
                                }
                            }
                        }
                    }

                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;
                }
                else
                {
                    string currentKey = Session[tableName + "currentID"].ToString();
                    List<string> listCol = new List<string>();
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if (control is Label)
                        {
                            string label = ((Label)control).Text.Trim();
                            string subLabel = label.Substring(0, label.Length - 1);
                            listCol.Add(subLabel);
                        }
                    }

                    DataTable preRec = dataBLL.GetPreviousRecord(tableName, listCol, currentKey);

                    if (preRec.Rows.Count > 0)
                    {
                        foreach (Control control in myPlaceHolder.Controls)
                        {
                            if ((control is TextBox) || (control is DropDownList))
                            {
                                foreach (DataColumn colName in preRec.Columns)
                                {
                                    if (control is TextBox)
                                    {
                                        ((TextBox)control).Text = preRec.Rows[0][colName].ToString();
                                    }
                                    else
                                    {
                                        ((DropDownList)control).SelectedValue = preRec.Rows[0][colName].ToString();
                                    }
                                    preRec.Columns.Remove(colName);
                                    break;
                                }
                            }
                        }
                    }

                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;

                }


            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            //string rs = dataBLL.Test();
            //if(string.IsNullOrEmpty(rs))
            //{
            //    Response.Write("Null");
            //}
            //else
            //{
            //    Response.Write(rs);
            //}
            if (Request.QueryString["tableName"] != null)
            {
                string tableName = Request.QueryString["tableName"];
                string tableNameSub = tableName.Substring(4);
                List<Dictionary<string, string>> input = new List<Dictionary<string, string>>();
                foreach (Control control in myPlaceHolder.Controls)
                {
                    if ((control is TextBox) || (control is DropDownList))
                    {
                        if (control is TextBox)
                        {
                            string colName = ((TextBox)control).ID.ToString().Substring(3);
                            string colValue = ((TextBox)control).Text.Trim();
                            Dictionary<string, string> child = new Dictionary<string, string>();
                            child.Add(colName, colValue);
                            input.Add(child);
                        }
                        else
                        {
                            string colName = ((DropDownList)control).ID.ToString().Substring(3);
                            string colValue = ((DropDownList)control).SelectedValue.ToString().Trim();
                            Dictionary<string, string> child = new Dictionary<string, string>();
                            child.Add(colName, colValue);
                            input.Add(child);
                        }
                    }
                }

                DataTable dtNextRec = dataBLL.Delete(tableName, input);
                if (dtNextRec != null)
                {
                    foreach (Control control in myPlaceHolder.Controls)
                    {
                        if ((control is TextBox) || (control is DropDownList))
                        {
                            foreach (DataColumn colName in dtNextRec.Columns)
                            {
                                if (control is TextBox)
                                {
                                    ((TextBox)control).Text = dtNextRec.Rows[0][colName].ToString();
                                }
                                else
                                {
                                    ((DropDownList)control).SelectedValue = dtNextRec.Rows[0][colName].ToString();
                                }
                                dtNextRec.Columns.Remove(colName);
                                break;
                            }
                        }
                    }
                    Session[tableName + "currentID"] = ((TextBox)myPlaceHolder.FindControl("txt" + tableNameSub + "ID")).Text;
                }

            }

            
        }


    }
}